<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.sku" placeholder="货号" clearable style="width: 130px;" class="filter-item" />
      <el-input v-model="listQuery.barcode" placeholder="条形码" clearable style="width: 130px; margin-left: 10px;" class="filter-item" />
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves :loading="downloadLoading" class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-download" @click="handleDownload">
        导出
      </el-button>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-refresh" @click="handleRefresh">
        重置
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-circle-plus-outline" @click="handleAdd">
        添加
      </el-button>
      <el-upload
        v-loading="uploadLoading"
        class="avatar-uploader filter-item"
        action="#"
        :show-file-list="false"
        :before-upload="beforeSizeUpload"
        :http-request="sizeUpload"
        style="margin-left: 10px;"
      >
        <el-button type="info" icon="el-icon-upload">
          上传
        </el-button>
      </el-upload>
      <el-button :loading="downloadTemplateLoading" class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-document" @click="handleDownloadTemplate">
        模板下载
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" border stripe element-loading-text="拼命加载中">
      <el-table-column label="货号" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.sku }}</span>
        </template>
      </el-table-column>
      <el-table-column label="尺码" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span style="float: right;">{{ row.normalizedSize }}</span>
        </template>
      </el-table-column>
      <el-table-column label="条形码" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.barcode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="140px" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <router-link :to="'/manager/goods/barcode/'+row.id+'/edit'">
            <el-button type="info" size="mini">
              编辑
            </el-button>
          </router-link>
          <el-button type="danger" size="mini" style="margin-left: 5px;" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog title="尺码添加" :visible.sync="addVisible">
      <el-form ref="addForm" :rules="addRules" :model="add" label-width="80px" style="margin-left:0px;">
        <el-form-item label="货号" prop="sku">
          <el-input v-model="add.sku" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="尺码" prop="normalizedSize">
          <el-input v-model="add.normalizedSize" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="条形码" prop="barcode">
          <el-input v-model="add.barcode" style="width: 200px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取消</el-button>
        <el-button type="primary" @click="addConfirm()">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { getBarcodeByPage, addBarcode, deleteBarcodeById } from '@/api/manager/goods-api'
import { downloadBarcode, uploadBarcode, downloadBarcodeTemplate } from '@/api/manager/goods-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { handleDownloadWithUtf8Filename } from '@/utils/index'

export default {
  name: 'ManagerBarcode',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      beforeRouteEnterNextIgnore: true,

      listQuery: {
        pageNo: 1,
        pageSize: 20,
        sku: null,
        barcode: null
      },
      downloadLoading: false,
      uploadLoading: false,
      downloadTemplateLoading: false,

      listLoading: true,
      list: null,
      total: 0,

      addVisible: false,
      add: {
        sku: null,
        normalizedSize: null,
        barcode: null
      },
      addRules: {
        sku: [{
          required: true, message: '请输入货号', trigger: 'blur'
        }],
        normalizedSize: [{
          required: true, message: '请输入尺码', trigger: 'blur'
        }],
        barcode: [{
          required: true, message: '请输入条形码', trigger: 'blur'
        }]
      }

    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (!vm.beforeRouteEnterNextIgnore) {
        vm.getList()
      } else {
        vm.beforeRouteEnterNextIgnore = false
      }
    })
  },
  async created() {
    await this.initLoad()
    await this.getList()
  },
  methods: {
    async initLoad() {
      this.initListQuery()
    },
    getList() {
      this.listLoading = true
      getBarcodeByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.listLoading = false
      })
    },
    initListQuery() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        sku: null,
        normalizedSize: null,
        barcode: null
      }
    },

    handleFilter() {
      this.getList()
    },
    handleDownload() {
      this.downloadLoading = true
      downloadBarcode(this.listQuery).then(response => {
        handleDownloadWithUtf8Filename(response)
        this.downloadLoading = false
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.downloadLoading = false
      })
    },
    handleRefresh() {
      this.initListQuery()
      this.getList()
    },
    handleAdd() {
      this.add = {
        sku: null,
        normalizedSize: null,
        barcode: null
      }
      this.addVisible = true
    },
    beforeSizeUpload(file) {
      var pass = true
      return pass
    },
    sizeUpload(param, type) {
      this.uploadLoading = true
      var formData = new FormData()
      formData.append('file', param.file)
      uploadBarcode(formData).then(response => {
        if (response && response.data && response.data.barcodeCount) {
          Message({
            message: '成功上传条形码信息' + response.data.barcodeCount + '条',
            type: 'success',
            duration: 5 * 1000
          })
          this.handleRefresh()
        } else {
          Message({
            message: response.msg,
            type: 'error',
            duration: 5 * 1000
          })
        }
        this.uploadLoading = false
      }).catch(error => {
        if (error) {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        } else {
          Message({
            message: '文件上传失败',
            type: 'error',
            duration: 5 * 1000
          })
        }
        this.uploadLoading = false
      })
    },
    handleDownloadTemplate() {
      this.downloadTemplateLoading = true
      downloadBarcodeTemplate().then(response => {
        handleDownloadWithUtf8Filename(response)
        this.downloadTemplateLoading = false
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.downloadTemplateLoading = false
      })
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },
    handleDelete(row) {
      deleteBarcodeById(row.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    },

    addConfirm() {
      this.$refs.addForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.add))
        addBarcode(data).then(response => {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
          this.addVisible = false
          this.getList()
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    }

  }
}

</script>
